/*
	DebugFunctions.cpp

	contains misc functions used to assist in debugging

	Author: Aidan Goddard 26/8/14
*/

#include"Lock.h"
#include"Dispatch.h"
#include"EngineState.h"
#include<iostream>

// engine safe string print function
static uint32_t lock_structure[1024] = {0};
static uint32_t *lock = &(lock_structure[128]);
SNOWSTORMXDLL_API void PrintString(uint32_t thread_ID, wchar_t *string)
{
	// get lock
	SpinEnter(lock);

	// copy the string to the engine's debug buffer
	int i = 0;
	int k = Snowstorm::EngineState::engine_state.debug_buffer_length;
	wchar_t *b = Snowstorm::EngineState::engine_state.debug_buffer;
	while((k < DEBUG_BUFFER_LENGTH_MAX) && (string[i] != 0))
	{
		b[k++] = string[i++];
	}
	if(k < DEBUG_BUFFER_LENGTH_MAX)
	{
		b[k] = 0;
	}
	else
	{
		b[k - 1] = 0;
	}
	Snowstorm::EngineState::engine_state.debug_buffer_length = k;

	// release lock
	SpinLeave(lock);

	// finished
	return;
}